package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public boolean add(Brand brand) {
        return brandMapper.insert(brand) > 0;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        return brandMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return brandMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(Brand brand) {
        return brandMapper.updateById(brand) > 0;
    }

    @Override
    public Brand findById(int id) {
        return brandMapper.selectById(id);
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(Brand brand, int currentPage, int pageSize) {
        LambdaQueryWrapper<Brand> lqw = new LambdaQueryWrapper<>();
        lqw.like(brand.getStatus() != null, Brand::getStatus, brand.getStatus())
                .like(brand.getBrandName() != null, Brand::getBrandName, brand.getBrandName())
                .like(brand.getCompanyName() != null, Brand::getCompanyName, brand.getCompanyName());
//        QueryWrapper<Brand> lqw = new QueryWrapper<>();
//        lqw.like(brand.getStatus() != null, "brand_name", brand.getStatus())
//                .like(brand.getBrandName() != null, "company_name", brand.getBrandName());
        Page<Brand> brandPage = new Page<>(currentPage, pageSize);
        Page<Brand> brandIPage = brandMapper.selectPage(brandPage, lqw);
        return new PageBean<Brand>(brandIPage.getTotal(), brandIPage.getRecords());
    }

}
